<script>
import {uploadFile} from "@/api/aihuman/common/minio";
import {getUrl} from "@/util/aihuman/getUrl";

export default {
  name: "humanInfo",
  data() {
    return {
      form: {
        aimetaName: '',
        aimetaUse: '2',
        dimensionType: '1',
        aimetaTag: '',
        status: 0,
        aimetaAvatar: ''
      },
      rules: {
        aimetaName: [
          {required: true, message: "请输入数字人名称", trigger: "blur"},
          {min: 1, max: 20, message: "长度不可超过20个字符", trigger: "blur"}
        ],
        aimetaUse: [
          {required: true, message: "请选择数字人用途", trigger: "change"},
        ],
        dimensionType: [
          {required: true, message: "请选择数字人类型", trigger: "change"},
        ],
      },
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
    }
  },
  methods: {
    getUrl,
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      this.form.aimetaTag = this.dynamicTags.join(',')
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    async uploadHumanAvatar(file) {
      if (file.file.type !== 'image/png') {
        this.$message.error('请上传png格式的图片')
        return false
      }

      let formData = new FormData()
      formData.append('file', file.file)
      formData.append('keyFolderPath', 'humanManagement/humanConfig')

      const {data} = await uploadFile(formData)
      this.form.aimetaAvatar = data.data.filePath

    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue.length > 10) {
        this.$message.error('标签字数最长为10个')
        return false
      }

      if (inputValue.length > 0 && this.form.aimetaTag.indexOf(inputValue) !== -1) {
        this.$message.error('标签已存在，请修改')
        return false
      }

      if (inputValue) {
        this.dynamicTags.push(inputValue);
        this.form.aimetaTag = this.dynamicTags.join(',')
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>

<template>
  <div class="human-info">
    <h3>基本信息</h3>

    <el-form ref="form" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="数字人名称" prop="aimetaName">
        <el-input v-model="form.aimetaName"></el-input>
      </el-form-item>
      <el-form-item label="用途" prop="aimetaUse">
        <el-radio-group v-model="form.aimetaUse">
          <el-radio label="1">数字解说员</el-radio>
          <el-radio label="2">智能交互</el-radio>
        </el-radio-group>
      </el-form-item>

<!--      <el-form-item label="类型" prop="dimensionType">
        <el-radio-group v-model="form.dimensionType" disabled>
          <el-radio label="3">web3D</el-radio>
          <el-radio label="4">UE</el-radio>
        </el-radio-group>
      </el-form-item>-->

      <el-form-item label="数字人标签">
        <el-tag
          :key="tag"
          v-for="tag in dynamicTags"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-switch
          v-model="form.status"
          :inactive-value="1"
          :active-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="数字人头像">
        <el-upload
          class="avatar-uploader"
          action=""
          :http-request="uploadHumanAvatar"
          accept=".png"
          :show-file-list="false">
          <img v-if="form.aimetaAvatar" :src="getUrl()+form.aimetaAvatar" class="avatar">
          <span v-else>上传头像</span>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.avatar-uploader {
  background-color: #F5F8FF;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 120px;
  width: 120px;
  cursor: pointer;
  overflow: hidden;

  img {
    height: 120px;
    width: 120px;
    object-fit: contain;
  }
}
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}


.human-uploader {
  background: #F5F8FF;
  height: 450px;
  width: 400px;

  /deep/ .el-upload {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    i {
      font-size: 40px;
      color: #999;
    }
  }
}

.human-upload {
  margin-right: 20px;
}

.human-info {
  display: flex;
  flex-direction: column;
}
</style>
